package com.gzk.oeno.business.opinion;

import java.util.Collection;


public class OpinionManager {

	private static final OpinionManager INSTANCE = new OpinionManager();
	
	public static final OpinionManager getInstance(){
		return INSTANCE;
	}
	

	private OpinionManager() {}
	
	
	public MeanOpinionBean calculateMeanInfos(Collection<Opinion> opinions){
		if (opinions == null)
			return null;
		
		int detailsNb = 0;
		int sum = 0;
		for (Opinion opinion : opinions){
			if (opinion.hasDetails())
				detailsNb++;
			sum += opinion.getScore().intValue();
		}
		
		Float mean = null;
		if (opinions.size() > 0){
			mean = Float.valueOf(sum / (float) opinions.size());
		}
		
		MeanOpinionBean result = new MeanOpinionBean();
		result.meanRate = mean;
		result.nbRates = opinions.size();
		result.nbDetailedRates = detailsNb;
		return result;
	}
}
